package com.tiza.xiaoyoubao.ui.fragment;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianze.library.base.BaseFragment;
import com.tianze.library.view.SimpleDialog;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.AppContext;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mapple on 17/3/5.
 * 设置
 */

public class SetUpFragment extends BaseFragment {

    @BindView(R.id.iv_voice)
    ImageView iv_voice;
    @BindView(R.id.iv_vibration)
    ImageView iv_vibration;
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setup;
    }

    @Override
    protected void initData() {
        long fileSize = 0;
        File cacheDir = mActivity.getCacheDir();
        fileSize += getDirSize(cacheDir);
        Log.i("缓存"," "+fileSize);
    }
    /**
     * 获取目录文件大小
     *
     * @param dir
     * @return
     */
    public static long getDirSize(File dir) {
        if (dir == null) {
            return 0;
        }
        if (!dir.isDirectory()) {
            return 0;
        }
        long dirSize = 0;
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                dirSize += file.length();
            } else if (file.isDirectory()) {
                dirSize += file.length();
                dirSize += getDirSize(file); // 递归调用继续统计
            }
        }
        return dirSize;
    }
    @Override
    protected void initView() {
        if (AppContext.getInstance().isSound()) {
            iv_voice.setBackgroundResource(R.mipmap.btnopen);
        } else {
            iv_voice.setBackgroundResource(R.mipmap.btnclose);
        }
        if (AppContext.getInstance().isVibration()) {
            iv_vibration.setBackgroundResource(R.mipmap.btnopen);
        } else {
            iv_vibration.setBackgroundResource(R.mipmap.btnclose);
        }
    }

    @OnClick({
            R.id.view_changePassWord,
            R.id.view_cleanCache,
            R.id.iv_bubbles,
            R.id.iv_voice,
            R.id.iv_vibration
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_changePassWord:
                toast("暂未开放");
                break;
            case R.id.view_cleanCache:
                toast("暂未开放");
                break;
            case R.id.iv_bubbles:
                toast("暂未开放");
                break;
            case R.id.iv_voice:
                if (AppContext.getInstance().isSound()) {
                    iv_voice.setBackgroundResource(R.mipmap.btnclose);
                    showCustomConfirmDialog("关闭声音提醒可能会导致超时未提交任务而影响完成率哦,确定要关闭吗?", "确认关闭", "取消",
                                            new SimpleDialog.OnSimpleClickListener() {
                                                @Override
                                                public void onClick(SimpleDialog simpleDialog) {
                                                    AppContext.getInstance().setSound(false);
                                                    simpleDialog.dismiss();
                                                }
                                            }, new SimpleDialog.OnSimpleClickListener() {
                                @Override
                                public void onClick(SimpleDialog simpleDialog) {
                                    iv_voice.setBackgroundResource(R.mipmap.btnopen);
                                    simpleDialog.dismiss();
                                }
                            });
                } else {
                    iv_voice.setBackgroundResource(R.mipmap.btnopen);
                    AppContext.getInstance().setSound(true);
                }
                break;
            case R.id.iv_vibration:
                if (AppContext.getInstance().isVibration()) {
                    iv_vibration.setBackgroundResource(R.mipmap.btnclose);
                    showCustomConfirmDialog("关闭振动提醒可能会导致超时未提交任务而影响完成率哦,确定要关闭吗?", "确认关闭", "取消",
                                            new SimpleDialog.OnSimpleClickListener() {
                        @Override
                        public void onClick(SimpleDialog simpleDialog) {
                            AppContext.getInstance().setVibration(false);
                            simpleDialog.dismiss();
                        }
                    }, new SimpleDialog.OnSimpleClickListener() {
                        @Override
                        public void onClick(SimpleDialog simpleDialog) {
                            iv_vibration.setBackgroundResource(R.mipmap.btnopen);
                            simpleDialog.dismiss();
                        }
                    });

                } else {
                    iv_vibration.setBackgroundResource(R.mipmap.btnopen);
                    AppContext.getInstance().setVibration(true);
                }
                break;

            default:
                break;
        }
    }
}
